home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / expect / lib_exp.c < prev    next >
C/C++ Source or Header  |  1993-03-29  |  14KB  |  600 lines

  1. /* lib_exp.c - top-level functions in the expect C library, libexpect.a
  2.  
  3. Written by: Don Libes, libes@cme.nist.gov, NIST, 12/3/90
  4.  
  5. Design and implementation of this program was paid for by U.S. tax
  6. dollars.  Therefore it is public domain.  However, the author and NIST
  7. would appreciate credit if this program or parts of it are used.
  8. */
  9.  
  10. #include "exp_conf.h"
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13. #include <sys/ioctl.h>
  14. #ifdef HAVE_SYS_FCNTL_H
  15. #  include <sys/fcntl.h>
  16. #else
  17. #  include <fcntl.h>
  18. #endif
  19. #include <signal.h>
  20. /*#include <memory.h> - deprecated - ANSI C moves them into string.h */
  21. #ifndef NO_STRING_H
  22. #include <string.h>
  23. #endif
  24. #include <varargs.h>
  25. #include <errno.h>
  26. #include "exp_rename.h"
  27. #define EXP_DEFINE_FNS
  28. #include "expect.h"
  29.  
  30. extern char *sys_errlist[];
  31. extern int errno;
  32.  
  33. #ifdef NO_MEMCPY
  34. #define memcpy(x,y,len) bcopy(y,x,len)
  35. #endif
  36.  
  37. /* avoid coliding with Tcl's stdlib.h */
  38. #ifndef _STDLIB
  39. char *malloc();
  40. void exit();
  41. #endif
  42.  
  43. #ifndef TRUE
  44. #define TRUE 1
  45. #define FALSE 0
  46. #endif
  47.  
  48. #define EXP_MATCH_MAX    2000
  49. /* public */
  50. char *exp_match = 0;
  51. int exp_match_max = EXP_MATCH_MAX;    /* bytes */
  52. int exp_timeout = 10;            /* seconds */
  53. int exp_autoallocpty = TRUE;        /* if TRUE, we do allocation */
  54. int exp_pty[2];                /* master is [0], slave is [1] */
  55. int exp_pid;
  56. char *exp_stty_init = 0;        /* initial stty args */
  57. int exp_ttycopy = TRUE;            /* copy tty parms from /dev/tty */
  58. int exp_ttyinit = TRUE;            /* set tty parms to sane state */
  59. int exp_disconnected = FALSE;        /* not disc. from controlling tty */
  60.  
  61. jmp_buf exp_readenv;        /* for interruptable read() */
  62. int exp_reading = FALSE;    /* whether we can longjmp or not */
  63.  
  64. extern FILE *debugfile;
  65. extern FILE *logfile;
  66. int logfile_all = FALSE;
  67. int loguser = FALSE;
  68.  
  69. void debuglog();
  70. int getptymaster();
  71. int getptyslave();
  72. int exp_stringmatch();
  73.  
  74. #define sysreturn(x)    return(errno = x, -1)
  75.  
  76. void init_pty();
  77.  
  78. /* returns fd of master side of pty */
  79. int
  80. exp_spawnv(file,argv)
  81. char *file;
  82. char *argv[];    /* some compiler complains about **argv? */
  83. {
  84.     int ttyfd;
  85.  
  86.     static int first_time = TRUE;
  87.  
  88.     if (first_time) {
  89.         first_time = FALSE;
  90.         init_pty();
  91.     }
  92.  
  93.     if (!file || !argv) sysreturn(EINVAL);
  94.     if (!argv[0] || strcmp(file,argv[0])) {
  95.         debuglog("expect: warning: file (%s) != argv[0] (%s)\n",
  96.             file,
  97.             argv[0]?argv[0]:"");
  98.     }
  99.  
  100.     if (exp_autoallocpty) {
  101.         if (0 > (exp_pty[0] = getptymaster())) sysreturn(ENODEV);
  102.     }
  103.     fcntl(exp_pty[0],F_SETFD,1);    /* close on exec */
  104.     if ((exp_pid = fork()) == -1) return(-1);
  105.     if (exp_pid) {
  106.         /* parent */
  107.         if (!exp_autoallocpty) close(exp_pty[1]);
  108.         return(exp_pty[0]);
  109.     }
  110.  
  111.     /* child process - do not return from here!  all errors must exit() */
  112.  
  113. #ifdef POSIX
  114.     setsid();
  115. #else
  116. #ifdef SYSV3
  117.     setpgrp();
  118. #else /* !SYSV3 */
  119. #ifdef MIPS_BSD
  120.     /* required on BSD side of MIPS OS <jmsellen@watdragon.waterloo.edu> */
  121. #    include <sysv/sys.s>
  122.     syscall(SYS_setpgrp);
  123. #endif
  124.     /* if (exp_disconnected) */
  125.     setpgrp(0,0);/* make a new pgrp leader */
  126.     ttyfd = open("/dev/tty", O_RDWR);
  127.     if (ttyfd >= 0) {
  128.         (void) ioctl(ttyfd, TIOCNOTTY, (char *)0);
  129.         (void) close(ttyfd);
  130.     }
  131. #endif /* SYSV3 */
  132. #endif /* POSIX */
  133.     if (exp_autoallocpty) {
  134.         close(0);
  135.         close(1);
  136.         /* leave 2 around awhile for stderr-related stuff */
  137.  
  138.         /* since we closed fd 0, open of pty slave must return fd 0 */
  139.         if (0 > (exp_pty[1] = getptyslave(exp_ttycopy,exp_ttyinit,
  140.                         exp_stty_init))) {
  141.         fprintf(stderr,"open(slave pty): %s\n",sys_errlist[errno]);
  142.         exit(-1);
  143.         }
  144.         /* sanity check */
  145.         if (exp_pty[1] != 0) {
  146.         fprintf(stderr,"getptyslave: slave = %d but expected 0\n",
  147.                                 exp_pty[1]);
  148.         exit(-1);
  149.         }
  150.     } else {
  151.         if (exp_pty[1] != 0) {
  152.             close(0);
  153.             fcntl(exp_pty[1],F_DUPFD,0);
  154.         }
  155.         close(1);
  156.         fcntl(0,F_DUPFD,1);
  157.         close(exp_pty[1]);
  158.     }
  159.  
  160.     /* by now, fd 0 and 1 point to slave pty, so fix 2 */
  161.     close(2);
  162.     fcntl(0,F_DUPFD,2);    /* duplicate 0 onto 2 */
  163.  
  164.     /* (possibly multiple) masters are closed automatically due to */
  165.     /* earlier fcntl(,,CLOSE_ON_EXEC); */
  166.  
  167.         (void) execvp(file,argv);
  168.     /* Unfortunately, by now we've closed fd's to stderr, logfile and
  169.         debugfile.
  170.        The only reasonable thing to do is to send back the error as
  171.        part of the program output.  This will be picked up in an
  172.        expect or interact command.
  173.     */
  174.     fprintf(stderr,"execvp(%s): %s\n",file,sys_errlist[errno]);
  175.     exit(-1);
  176.     /*NOTREACHED*/
  177. }
  178.  
  179. /* returns fd of master side of pty */
  180. /*VARARGS*/
  181. int
  182. exp_spawnl(va_alist)
  183. va_dcl
  184. {
  185.     va_list args;
  186.     int i;
  187.     char *arg, **argv;
  188.  
  189.     va_start(args);
  190.     for (i=0;;i++) {
  191.         arg = va_arg(args,char *);
  192.         if (!arg) break;
  193.     }
  194.     va_end(args);
  195.     if (i == 0) sysreturn(EINVAL);
  196.     if (!(argv = (char **)malloc((i+1)*sizeof(char *)))) sysreturn(ENOMEM);
  197.     va_start(args);
  198.     for (i=0;;i++) {
  199.         argv[i] = va_arg(args,char *);
  200.         if (!argv[i]) break;
  201.     }
  202.     i = exp_spawnv(argv[0],argv+1);
  203.     free((char *)argv);
  204.     return(i);
  205. }
  206.  
  207. /* remove nulls from s.  Initially, the number of chars in s is c, */
  208. /* not strlen(s).  This count does not include the trailing null. */
  209. /* returns number of nulls removed. */
  210. static int
  211. rm_nulls(s,c)
  212. char *s;
  213. int c;
  214. {
  215.     char *s2 = s;    /* points to place in original string to put */
  216.             /* next non-null character */
  217.     int count = 0;
  218.     int i;
  219.  
  220.     for (i=0;i<c;i++,s++) {
  221.         if (0 == *s) {
  222.             count++;
  223.             continue;
  224.         }
  225.         if (count) *s2 = *s;
  226.         s2++;
  227.     }
  228.     return(count);
  229. }
  230.  
  231. /* generate printable versions of random ASCII strings.  This is used by */
  232. /* cmdExpect when -d forces it to print strings it is examining. */
  233. static char *
  234. printify(s)
  235. char *s;
  236. {
  237.     static int destlen = 0;
  238.     static char *dest = 0;
  239.     char *d;        /* ptr into dest */
  240.     unsigned int need;
  241.  
  242.     /* worst case is every character takes 3 to printify */
  243.     need = strlen(s)*3 + 1;
  244.     if (need > destlen) {
  245.         if (dest) free(dest);
  246.         if (!(dest = malloc(need))) {
  247.             destlen = 0;
  248.             return("malloc failed in printify");
  249.         }
  250.         destlen = need;
  251.     }
  252.  
  253.     for (d = dest;*s;s++) {
  254.         if (*s == '\r') {
  255.             strcpy(d,"\\r");        d += 2;
  256.         } else if (*s == '\n') {
  257.             strcpy(d,"\\n");        d += 2;
  258.         } else if (*s == '\t') {
  259.             strcpy(d,"\\t");        d += 2;
  260.         } else if ((unsigned)*s < 0x20) { /* unsigned strips parity */
  261.             sprintf(d,"^%c",*s + '@');    d += 3;
  262.         } else if (*s == 0x7f) {
  263.             /* not syntactically correct, but you get the point */
  264.             strcpy(d,"\\7f");        d += 3;
  265.         } else {
  266.             *d = *s;            d += 1;
  267.         }
  268.     }
  269.     *d = '\0';
  270.     return(dest);
  271. }
  272.  
  273. static int i_read_errno;/* place to save errno, if i_read() == -1, so it
  274.                doesn't get overwritten before we get to read it */
  275.  
  276. /*ARGSUSED*/
  277. static void
  278. sigalarm_handler(n)
  279. int n;            /* signal number, unused by us */
  280. {
  281. #ifdef REARM_SIG
  282.     signal(SIGALRM,sigalarm_handler);
  283. #endif
  284.  
  285.     longjmp(exp_readenv,1);
  286. }
  287.  
  288. /* interruptable read */
  289. static int
  290. i_read(fd,fp,buffer,length,timeout)
  291. int fd;
  292. FILE *fp;
  293. char *buffer;
  294. int length;
  295. int timeout;
  296. {
  297.     int cc = -2;
  298.  
  299.     /* since setjmp insists on returning 1 upon longjmp(,0), */
  300.     /* longjmp(,2) instead. */
  301.  
  302.     alarm(timeout);
  303.  
  304.     /* restart read if setjmp returns 0 (first time) or 2. */
  305.     /* abort if setjmp returns 1. */
  306.     if (1 != setjmp(exp_readenv)) {
  307.         exp_reading = TRUE;
  308.         if (fd != -1) cc = read(fd,buffer,length);
  309.         else {
  310.             int c;
  311.             c = getc(fp);
  312.             if (c == EOF) {
  313.                 if (feof(fp)) cc = 0;
  314.                 else cc = -1;
  315.             } else {
  316.                 buffer[0] = c;
  317.                 cc = 1;
  318.             }
  319.         }
  320. #if 0
  321.         /* can't get fread to return early! */
  322.         else {
  323.             if (!(cc = fread(buffer,1,length,fp))) {
  324.                 if (ferror(fp)) cc = -1;
  325.             }
  326.         }
  327. #endif
  328.         i_read_errno = errno;    /* errno can be overwritten by the */
  329.                     /* time we return */
  330.     }
  331.     exp_reading = FALSE;
  332.  
  333.     alarm(0);
  334.     return(cc);
  335. }
  336.  
  337. static unsigned int bufsiz = 2*EXP_MATCH_MAX;
  338.  
  339. /* I tried really hard to make the following two functions share the code */
  340. /* that makes the ecase array, but I kept running into a brick wall when */
  341. /* passing var args into the funcs and then again into a make_cases func */
  342. /* I would very much appreciate it if someone showed me how to do it right */
  343.  
  344. /* takes pairs of args, with a final 0 arg */
  345. /* first element of pair is pattern, 2nd is int returned if pattern matches */
  346. /* returns negative value if error (or EOF/timeout) occurs */
  347. /* some negative values can also have an associated errno */
  348.  
  349. static int
  350. expectv(fd,fp,ecases)
  351. int fd;
  352. FILE *fp;
  353. struct exp_case *ecases;
  354. {
  355.     int cc = 0;    /* number of chars returned in a single read */
  356.     int rc = 0;    /* number of chars in exp_match[] */
  357.     int oldrc;
  358.  
  359.     unsigned int new_siz;    /* this is just match_max*2 for efficiency */
  360.     char *new_buf;
  361.     extern char *realloc();
  362.     struct exp_case *ec;    /* points to current ecase */
  363.  
  364.     time_t current_time;        /* current time (when we last looked)*/
  365.     time_t end_time;        /* future time at which to give up */
  366.  
  367.     static int first_time = TRUE;
  368.  
  369.     if (first_time) {
  370.         first_time = FALSE;
  371.         if (!(exp_match = malloc((unsigned)(bufsiz+1))))
  372.             sysreturn(ENOMEM);
  373.     }
  374.  
  375.     if (!ecases) sysreturn(EINVAL);
  376.  
  377.     /* get the latest buffer size.  Double the user input for two */
  378.     /* reasons.  1) Need twice the space in case the match */
  379.     /* straddles two bufferfuls, 2) easier to hack the division by */
  380.     /* two when shifting the buffers later on */
  381.     if (bufsiz != (new_siz = 2*exp_match_max)) {
  382.         if (0 == (new_buf = realloc(exp_match,new_siz+1)))
  383.             sysreturn(ENOMEM);
  384.         bufsiz = new_siz;
  385.         exp_match = new_buf;
  386.     }
  387.  
  388.     exp_match[0] = '\0';
  389.     signal(SIGALRM,sigalarm_handler);
  390.  
  391.     time(¤t_time);
  392.     /* if user sets timeout to 0 (i.e. poll), do the next best */
  393.     /* thing: wait only one second.  Eventually polling should be */
  394.     /* implemented right, but I don't consider it high priority */
  395.     /* at the moment, especially cause select command can do it */
  396.     end_time = current_time + ((exp_timeout == 0)?1:exp_timeout);
  397.  
  398.     for (;;) {
  399.         /* when buffer fills, copy second half over first and */
  400.         /* continue, so we can do matches over multiple buffers */
  401.         if (rc == bufsiz) {
  402.             memcpy(exp_match,&exp_match[bufsiz/2],bufsiz/2);
  403.             rc = bufsiz/2;
  404.         }
  405.  
  406.         cc = i_read(fd,fp,&exp_match[rc],bufsiz-rc,
  407.             end_time-current_time);
  408.  
  409.         if (cc == 0) return(EXP_EOF);        /* normal EOF */
  410.         else if (cc == -1) {            /* abnormal EOF */
  411.             /* ptys produce EIO upon EOF - sigh */
  412.             if (i_read_errno == EIO) {
  413.                 /* convert to EOF indication */
  414.                 return(EXP_EOF);
  415.             }
  416.             sysreturn(i_read_errno);
  417.         } else if (cc == -2) return(EXP_TIMEOUT);
  418.  
  419.         oldrc = rc;
  420.         rc += cc;
  421.  
  422.         if (logfile_all || (loguser && logfile)) {
  423.             fwrite(&exp_match[oldrc],1,cc,logfile);
  424.         }
  425.         if (loguser) fwrite(&exp_match[oldrc],1,cc,stdout);
  426.         if (debugfile) fwrite(&exp_match[oldrc],1,cc,debugfile);
  427.  
  428.         /* if we wrote to any logs, flush them */
  429.         if (debugfile) fflush(debugfile);
  430.         if (loguser) {
  431.             fflush(stdout);
  432.             if (logfile) fflush(logfile);
  433.         }
  434.  
  435.         /* remove nulls from input, so we can use C-style strings */
  436.         /* doing it here lets them be sent to the screen, just */
  437.         /*  in case they are involved in formatting operations */
  438.         rc -= rm_nulls(&exp_match[oldrc],cc);
  439.         /* cc should be decremented as well, but since it will not */
  440.         /* be used before being set again, there is no need */
  441.         exp_match[rc] = '\0';
  442.  
  443.         debuglog("expect: does {%s} match ",printify(exp_match));
  444.         /* pattern supplied */
  445.         for (ec=ecases;ec->pattern;ec++) {
  446.             debuglog("{%s}? ",printify(ec->pattern));
  447.             if (exp_stringmatch(exp_match,ec->pattern)) {
  448.                 debuglog("yes\nexp_match is {%s}\n",
  449.                             printify(exp_match));
  450.                 return(ec->value);
  451.             }
  452.             debuglog("no\n");
  453.         }
  454.     }
  455. }
  456.  
  457. int
  458. exp_fexpectv(fp,ecases)
  459. FILE *fp;
  460. struct exp_case *ecases;
  461. {
  462.     return(expectv(-1,fp,ecases));
  463. }
  464.  
  465. int
  466. exp_expectv(fd,ecases)
  467. int fd;
  468. struct exp_case *ecases;
  469. {
  470.     return(expectv(fd,(FILE *)0,ecases));
  471. }
  472.  
  473. /*VARARGS*/
  474. int
  475. exp_expectl(va_alist)
  476. va_dcl
  477. {
  478.     va_list args;
  479.     int fd;
  480.     struct exp_case *ec, *ecases;
  481.     int i;
  482.  
  483.     va_start(args);
  484.     fd = va_arg(args,int);
  485.     /* first just count the arg-pairs */
  486.     for (i=0;;i++) {
  487.         if (!va_arg(args,char *)) break;
  488.         va_arg(args,int);    /*COMPUTED, BUT NOT USED*/
  489.     }
  490.     va_end(args);
  491.  
  492.     if (!(ecases = (struct exp_case *)
  493.                 malloc((1+i)*sizeof(struct exp_case))))
  494.         sysreturn(ENOMEM);
  495.  
  496.     va_start(args);
  497.     va_arg(args,int);        /*COMPUTED, BUT NOT USED*/
  498.     for (ec=ecases;;ec++) {
  499.         if (!(ec->pattern = va_arg(args,char *))) break;
  500.         ec->value = va_arg(args,int);
  501.     }
  502.     va_end(args);
  503.     i = expectv(fd,(FILE *)0,ecases);
  504.     free((char *)ecases);
  505.     return(i);
  506. }
  507.  
  508. int
  509. exp_fexpectl(va_alist)
  510. va_dcl
  511. {
  512.     va_list args;
  513.     FILE *fp;
  514.     struct exp_case *ec, *ecases;
  515.     int i;
  516.  
  517.     va_start(args);
  518.     fp = va_arg(args,FILE *);
  519.     /* first just count the arg-pairs */
  520.     for (i=0;;i++) {
  521.         if (!va_arg(args,char *)) break;
  522.         va_arg(args,int);    /*COMPUTED, BUT NOT USED*/
  523.     }
  524.     va_end(args);
  525.  
  526.     if (!(ecases = (struct exp_case *)
  527.                     malloc((1+i)*sizeof(struct exp_case))))
  528.         sysreturn(ENOMEM);
  529.  
  530.     va_start(args);
  531.     va_arg(args,FILE *);        /*COMPUTED, BUT NOT USED*/
  532.     for (ec=ecases;;ec++) {
  533.         if (!(ec->pattern = va_arg(args,char *))) break;
  534.         ec->value = va_arg(args,int);
  535.     }
  536.     va_end(args);
  537.     i = expectv(-1,fp,ecases);
  538.     free((char *)ecases);
  539.     return(i);
  540. }
  541.  
  542. /* like popen(3) but works in both directions */
  543. FILE *
  544. exp_popen(program)
  545. char *program;
  546. {
  547.     FILE *fp;
  548.     int ec;
  549.  
  550.     if (0 > (ec = exp_spawnl("sh","sh","-c",program,(char *)0))) return(0);
  551.     if (!(fp = fdopen(ec,"r+"))) return(0);
  552.     setbuf(fp,(char *)0);
  553.     return(fp);
  554. }
  555.  
  556. int
  557. exp_disconnect()
  558. {
  559.     int ttyfd;
  560.  
  561. #ifndef EALREADY
  562. #define EALREADY 37
  563. #endif
  564.  
  565.     /* presumably, no stderr, so don't bother with error message */
  566.     if (exp_disconnected) sysreturn(EALREADY);
  567.     exp_disconnected = TRUE;
  568.  
  569.     freopen("/dev/null","r",stdin);
  570.     freopen("/dev/null","w",stdout);
  571.     freopen("/dev/null","w",stderr);
  572.  
  573. #ifdef POSIX
  574.     setsid();
  575. #else
  576. #ifdef SYSV3
  577.     /* put process in our own pgrp, and lose controlling terminal */
  578.     setpgrp();
  579.     signal(SIGHUP,SIG_IGN);
  580.     if (fork()) exit(0);    /* first child exits (as per Stevens, */
  581.     /* UNIX Network Programming, p. 79-80) */
  582.     /* second child process continues as daemon */
  583. #else /* !SYSV3 */
  584. #ifdef MIPS_BSD
  585.     /* required on BSD side of MIPS OS <jmsellen@watdragon.waterloo.edu> */
  586. #    include <sysv/sys.s>
  587.     syscall(SYS_setpgrp);
  588. #endif
  589.     setpgrp(0,getpid());    /* put process in our own pgrp */
  590.     ttyfd = open("/dev/tty", O_RDWR);
  591.     if (ttyfd >= 0) {
  592.         /* zap controlling terminal if we had one */
  593.         (void) ioctl(ttyfd, TIOCNOTTY, (char *)0);
  594.         (void) close(ttyfd);
  595.     }
  596. #endif /* SYSV3 */
  597. #endif /* POSIX */
  598.     return(0);
  599. }
  600.